home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Classes / RCString / RCStringComp.m < prev    next >
Text File  |  1995-06-12  |  2KB  |  75 lines

  1. #import <RCString.h>
  2. /*
  3.     Copyright (C) 1992. Bruce Ediger.
  4.  
  5.       This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU Library General Public License.
  7. */
  8.  
  9. //
  10. // implementation of "comparison" methods
  11. //
  12.  
  13. // Is it better to use strcmp() or bcmp()?  bcmp()
  14. // may be faster since it can block out the comparison
  15. // looping.  Or is a custom routine better?
  16.  
  17. @implementation RCString (Comparison)
  18.  
  19. // returns
  20. //  < 0 if this object is "before" anotherObject
  21. //    0 if this object is "same as" anotherObject
  22. //  > 0 if this object is "in front of" anotherObject
  23. //
  24. // should the comparison be case-insensitive if _either_
  25. // object is marked as case-insensitive?
  26. - (int)compareWithObject: (RCString *)anotherObject
  27. {
  28.     int irRetval;
  29.  
  30.     if (p == NULL || p->s == NULL || anotherObject->p == NULL
  31.         || anotherObject->p->s == NULL)
  32.             return 0;
  33.  
  34.     // one quick special case: are references to the same internal rep?
  35.     if (p->n > 1 && p == anotherObject->p)
  36.         return 0;
  37.  
  38.     if (!yCaseSensitive) {
  39.         RCString *oThisObject, *oOtherObject;
  40.         oThisObject  = [RCString newFromObject:self];
  41.         oOtherObject = [RCString newFromObject:anotherObject];
  42.         [oThisObject  toUpper];
  43.         [oOtherObject toUpper];
  44.         irRetval = strcmp([oThisObject data], [oOtherObject data]);
  45.         [oThisObject  free];
  46.         [oOtherObject free];
  47.     } else {
  48.         irRetval = strcmp([self data], [anotherObject data]);
  49.     }
  50.     return irRetval;
  51. }
  52.  
  53. // returns
  54. //  < 0 if this object is "before" anAsciiString
  55. //    0 if this object is "same as" anAsciiString
  56. //  > 0 if this object is "in front of" anAsciiString
  57. - (int)compareWithString: (char *)anAsciiString
  58. {
  59.     int irRetval;
  60.     RCString *oTmp = [RCString newFromString:anAsciiString];
  61.     irRetval = [self compareWithObject:oTmp];
  62.     [oTmp free];
  63.     return irRetval;
  64. }
  65.  
  66. // set whether or not lexicographic comparisons
  67. // are case sensitive.
  68. - caseSensitive: (BOOL)isOrNot
  69. {
  70.     yCaseSensitive = isOrNot;
  71.     return self;
  72. }
  73.  
  74. @end
  75.